const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: {},
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    login:false,
    release:"jieDan faBu",
    my:"jieDan",
    conduct:"jixing",
    complete:"yiwan",
    list:{},
    type:''
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  jump:function(e){
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    var id = data[index]['id'];
    var type = that.data.type;
    wx.navigateTo({
      url: '../Uncertified/Uncertified?id=' + id +"&type=" + type,
    })
  },
  release:function(e){
    const that = this;
    if (e.currentTarget.dataset.type === "my"){
      app.util.request({
        url: 'Index/Index/myTakeOrders',
        data: {
          openid: app.globalData.userInfo.openid,
          is_complete:0,
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          if (res.data.code != 1){
            that.setData({
              list: res.data,
              type: "myTakeOrders"
            })
          }
        }
      })
      that.setData({
        release: "jieDan",
        my: "jieDan faBu",
        conduct: "jixing",
        complete: "yiwan"
      })
    }else{
      app.util.request({
        url: 'Index/Index/myReleaseOrders',
        data: {
          openid: app.globalData.userInfo.openid,
          is_complete: 0,
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data,
            type: "myReleaseOrders"
          })
        }
      })
      that.setData({
        release: "jieDan faBu",
        my: "jieDan",
        conduct: "jixing",
        complete: "yiwan"
      })
    }
  },

  status:function(e){
    const that= this;
    var type = that.data.type;
    if (e.currentTarget.dataset.type === "conduct") {
      app.util.request({
        url: 'Index/Index/' + type,
        data: {
          openid: app.globalData.userInfo.openid,
          is_complete: 0,
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data,
            type: "myReleaseOrders"
          })
        }
      })
      that.setData({
        conduct: "jixing",
        complete: "yiwan"
      })
    } else {
      app.util.request({
        url: 'Index/Index/' + type,
        data: {
          openid: app.globalData.userInfo.openid,
          is_complete: 1,
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data,
            type: "myReleaseOrders"
          })
        }
      })
      that.setData({
        conduct: "yiwan",
        complete: "jixing"
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    app.changeTabBar(); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindGetUserInfo: function (e) {
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(a) {
              var str = JSON.parse(a.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: res.data.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            console.log(res)
                            that.setData({
                              modalName: null
                            })
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    // 检验是否授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            login: true,
            modalName: 'Image'
          })
        } else {
          that.setData({
            login: false
          })
        }
      }
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              var str = JSON.parse(res.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        that.setData({
                          userInfo: res.data
                        })
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/myReleaseOrders',
                          data: {
                            openid: app.globalData.userInfo.openid,
                            is_complete: 0,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            that.setData({
                              list: res.data,
                              type: "myReleaseOrders",
                              release: "jieDan faBu",
                              my: "jieDan",
                              conduct: "jixing",
                              complete: "yiwan"
                            })
                          }
                        })
                        app.util.request({
                          url: 'Index/Index/ButtonNavList',
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            var data = res.data;
                            for (var i = 0; i < data.length; i++) {
                              data[i]['index'] = i;
                              data[i]['pagePath'] = data[i]['path'];
                              data[i]['text'] = data[i]['name'];
                              data[i]['iconPath'] = data[i]['before_image'];
                              data[i]['selectedIconPath'] = data[i]['after_image'];
                              data[i]['selected'] = false;
                            }
                            app.util.request({
                              url: 'Index/Index/getSumMessage',
                              data: {
                                openid: app.globalData.userInfo.openid,
                              },
                              header: {
                                'content-type': 'application/json'
                              },
                              success(res) {
                                data[2]['dott'] = res.data;
                                data[0]['selected'] = true;
                                app.globalData.tabbar.list = data;
                                app.setchangeTabBar();
                                that.setData({
                                  dottot: data[2]['dott']
                                })
                              }
                            })
                            data[0]['selected'] = true;
                            app.globalData.tabbar.list = res.data;
                            app.setchangeTabBar();
                          }
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: app.globalData.userInfo.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        } else {
          // wx.showToast({
          //   title: '请点击头像获取授权信息',
          //   icon: 'none',
          //   duration: 2000
          // });
          // return false;
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})